package com.marver.sys.service;


import com.marver.common.dao.BaseDAOImpl;
import com.marver.entity.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class LogService extends BaseDAOImpl {
	private Logger logger =LoggerFactory.getLogger(this.getClass());
	
	/**
	 * 寫日誌日誌
	 * @param log
	 * @return
	 */
	public Log addLog(Log log){
		this.addBySqlMap("common.addLog", log);
		return log;
	}   
	
	/**
	 * 寫日誌日誌
	 * @return
	 */
	public Log addLog(String remoteIp,String telNum,String apiAddress,String request){
		Log log=new Log();
		log.setCreateTime(new Date());
		log.setTelNum(telNum);
		log.setApiAddress(apiAddress);
		log.setRequest(request);
		log.setRemoteIp(remoteIp);
		this.addBySqlMap("common.addLog", log);
		return log;
	}
	
	/**
	 * 寫日誌日誌
	 * @return
	 */
	public Log addLog(String remoteIp,String telNum,String apiAddress,String request,String response){
		Log log=new Log();
		log.setCreateTime(new Date());
		log.setTelNum(telNum);
		log.setApiAddress(apiAddress);
		log.setRequest(request);
		log.setRemoteIp(remoteIp);
		log.setResponse(response);
		this.addBySqlMap("common.addLog", log);
		return log;
	}	
	
	/**
	 * 更新日誌
	 * @return
	 */
	public void updateLog(int id,String response){
		Log log=new Log();
		log.setUpdateTime(new Date());
		log.setId(id);
		log.setResponse(response);
		this.updateBySqlMap("common.updateLog", log);
	}
	
	/**
	 * 更新日誌
	 * @return
	 */
	public void updateLog(Log log){
		this.updateBySqlMap("common.updateLog", log);
	}

	/**
	 * 查询一分钟以内 同ip请求的次数
	 * @param apiAddress
	 * @param request
	 * @param remoteIp
	 * @return
	 */
	@Transactional(readOnly = true)
	public int getLogSum(String apiAddress, String request, String remoteIp){
		Log log=new Log();
		log.setApiAddress(apiAddress);
		log.setRequest(request);
		log.setRemoteIp(remoteIp);
		return (int)this.findObjectBySqlMap("common.getLogSum", log);
	}
}
